from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from database import SessionLocal
from crud import get_landmarks, get_routes
import random

# 创建路由器
router = APIRouter()

# 设置模板目录
templates = Jinja2Templates(directory="templates")

# 默认首页
@router.get("/", response_class=HTMLResponse)
async def index_page(request: Request):
    """默认首页"""
    return templates.TemplateResponse("index.html", {"request": request})

# 关于我们页面
@router.get("/about", response_class=HTMLResponse)
async def about_page(request: Request):
    """关于我们页面"""
    return templates.TemplateResponse("about.html", {"request": request})

# 登录页面
@router.get("/login", response_class=HTMLResponse)
async def login_page(request: Request):
    """登录页面"""
    return templates.TemplateResponse("admin/login.html", {"request": request})

# 探索页面
@router.get("/explore", response_class=HTMLResponse)
async def explore_page(request: Request):
    """探索页面"""
    return templates.TemplateResponse("explore.html", {"request": request})

# 探索页面数据API
@router.get("/api/getData")
async def get_explore_data(request: Request):
    """获取探索页面所需的地标和路径数据，不需要认证"""
    db = SessionLocal()
    try:
        # 获取所有地标
        landmarks_data = []
        landmarks = get_landmarks(db, skip=0, limit=1000)
        for landmark in landmarks:
            landmarks_data.append({
                "id": landmark.id,
                "name": landmark.name,
                "lat": landmark.lat,
                "lng": landmark.lng,
                "summary": landmark.summary,
                "details": landmark.details,
                "media": landmark.media,
                "time": landmark.time.strftime("%Y-%m-%d") if landmark.time else None
            })

        # 获取所有路径
        routes_data = []
        routes = get_routes(db, skip=0, limit=1000)
        for route in routes:
            landmark_ids = [landmark.id for landmark in route.landmarks]
            routes_data.append({
                "id": route.id,
                "name": route.name,
                "landmarks": landmark_ids
            })

        # 返回整合后的数据
        return {
            "landmarks": landmarks_data,
            "routes": routes_data
        }
    finally:
        db.close()

# 首页热门地点API
@router.get("/api/hotspots")
async def get_hotspots(request: Request):
    """获取首页热门地点数据，不需要认证"""
    db = SessionLocal()
    try:
        # 获取所有地标
        landmarks = get_landmarks(db, skip=0, limit=1000)

        # 随机选择3个地标作为热门地点
        hotspots = random.sample(landmarks, min(3, len(landmarks)))

        # 格式化返回数据
        hotspots_data = []
        for landmark in hotspots:
            hotspots_data.append({
                "id": landmark.id,
                "name": landmark.name,
                "summary": landmark.summary,
                "details": landmark.details,
                "media": landmark.media[0] if landmark.media and len(landmark.media) > 0 else None
            })

        return hotspots_data
    finally:
        db.close()
